package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AppInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.GameDetActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.MyCollectionListAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 我的收藏
 */
@SuppressLint("HandlerLeak")
public class MyCollectionFragment extends TitleBaseFragment {
    private Unbinder unbinder;

    @BindView(R.id.myshoulist)
    ListView myshoulist;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.springview)
    SpringView springview;
    private MyCollectionListAdapter myCollectionListAdapter;
    private int limit = 1;
    ArrayList<AppInfo> gameInfos = new ArrayList<>();

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_collection, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MyCollectionFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MyCollectionFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("我的收藏");

        myCollectionListAdapter = new MyCollectionListAdapter(getActivity(), this);
        myshoulist.setAdapter(myCollectionListAdapter);
        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
        springview.setHeader(new SimpleHeader(getContext()));
        springview.setFooter(new SimpleFooter(getContext()));
        myshoulist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<AppInfo> list = myCollectionListAdapter.getList();
                Intent intent = new Intent(getContext(), GameDetActivity.class);
                intent.putExtra("id", list.get(position).id + "");
                //intent.setClass(MyCollectionActivity.this,);
                startActivityForResult(intent, 11);
            }
        });
        initdata(limit);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 11)
            initdata(limit);
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            limit = 1;
            initdata(limit);
        }

        @Override
        public void onLoadmore() {
            limit = ++limit;
            initdata(limit);
        }
    };

    public void initdata(int p) {
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("token", loginUser.token);
            map.put("p", "" + p);
            if (p > 1) {
                HttpCom.POST(vhandler, HttpCom.getSCURL(), map, false);
            } else {
                HttpCom.POST(handler, HttpCom.getSCURL(), map, false);
            }
        }
    }

    /**
     * 我的收藏刚进来
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<AppInfo> dnssc = HttpUtils.DNSSC(msg.obj.toString());
                    if (dnssc != null) {
                        myshoulist.setVisibility(View.VISIBLE);
                        errorLayout.setVisibility(View.GONE);
                        gameInfos.clear();
                        gameInfos.addAll(dnssc);
                        myCollectionListAdapter.setList(gameInfos);
                    } else {
                        myshoulist.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("还没有收藏游戏");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
    /**
     * 加载更多
     */
    Handler vhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<AppInfo> dnssc = HttpUtils.DNSSC(msg.obj.toString());
                    if (dnssc != null) {
                        gameInfos.addAll(dnssc);
                        myCollectionListAdapter.setList(gameInfos);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
